package com.ruoyi.points.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.points.domain.QizhongKecheng;
import com.ruoyi.points.domain.QizhongPingjia;
import com.ruoyi.points.domain.QizhongStudent;
import com.ruoyi.points.domain.QizhongYuyue;
import com.ruoyi.points.domain.vo.QizhongKechengVo;
import com.ruoyi.points.mapper.QizhongKechengMapper;
import com.ruoyi.points.service.IQizhongKechengService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 期中课程Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-06
 */
@Service
public class QizhongKechengServiceImpl implements IQizhongKechengService {
    @Autowired
    private QizhongKechengMapper qizhongKechengMapper;

    /**
     * 查询期中课程
     *
     * @param id 期中课程主键
     * @return 期中课程
     */
    @Override
    public QizhongKecheng selectQizhongKechengById(Long id) {
        return qizhongKechengMapper.selectQizhongKechengById(id);
    }

    /**
     * 查询期中课程列表
     *
     * @param qizhongKecheng 期中课程
     * @return 期中课程
     */
    @Override
    public List<QizhongKechengVo> selectQizhongKechengList(QizhongKecheng qizhongKecheng) {
        List<QizhongKecheng> kechengs = qizhongKechengMapper.selectQizhongKechengList(qizhongKecheng);
        List<QizhongKechengVo> kechengVos = new ArrayList<>();
        for (QizhongKecheng kecheng : kechengs) {
            QizhongKechengVo vo = new QizhongKechengVo();
            int yunum = qizhongKechengMapper.selectyunum(kecheng.getId());
            int pingnum = qizhongKechengMapper.selectpingnum(kecheng.getId());
            BeanUtil.copyProperties(kecheng, vo);
            vo.setYunum((long) yunum);
            vo.setPingnum((long) pingnum);
            kechengVos.add(vo);
        }
        return kechengVos;
    }

    /**
     * 新增期中课程
     *
     * @param qizhongKecheng 期中课程
     * @return 结果
     */
    @Override
    public int insertQizhongKecheng(QizhongKecheng qizhongKecheng) {
        qizhongKecheng.setCreateTime(DateUtils.getNowDate());
        return qizhongKechengMapper.insertQizhongKecheng(qizhongKecheng);
    }

    /**
     * 修改期中课程
     *
     * @param qizhongKecheng 期中课程
     * @return 结果
     */
    @Override
    public int updateQizhongKecheng(QizhongKecheng qizhongKecheng) {
        return qizhongKechengMapper.updateQizhongKecheng(qizhongKecheng);
    }

    /**
     * 批量删除期中课程
     *
     * @param ids 需要删除的期中课程主键
     * @return 结果
     */
    @Override
    public int deleteQizhongKechengByIds(Long[] ids) {
        return qizhongKechengMapper.deleteQizhongKechengByIds(ids);
    }

    /**
     * 删除期中课程信息
     *
     * @param id 期中课程主键
     * @return 结果
     */
    @Override
    public int deleteQizhongKechengById(Long id) {
        return qizhongKechengMapper.deleteQizhongKechengById(id);
    }

    @Override
    public List<QizhongStudent> student() {
        return qizhongKechengMapper.student();
    }

    @Override
    public String yy(QizhongYuyue qizhongYuyue) {
        //查找是否已经预约过
        List<QizhongYuyue> list = qizhongKechengMapper.selectqizhongYuyue(qizhongYuyue.getSId());
        if (ObjectUtil.isNotEmpty(list)) {
            return "该学生已经预约过";
        } else {
            //添加预约
            qizhongKechengMapper.yy(qizhongYuyue);
            //查找课程表的价格
            QizhongKecheng qizhongKecheng = qizhongKechengMapper.selectQizhongKechengById(qizhongYuyue.getKId());
            //查找学生的信息
            QizhongStudent qizhongStudent = qizhongKechengMapper.getStudent(qizhongYuyue.getSId());
            //判断是不是VIP
            if (qizhongStudent.getIsVip() == 1) {
                qizhongStudent.setYuer(qizhongStudent.getYuer() - qizhongKecheng.getHprice());
            } else {
                qizhongStudent.setYuer(qizhongStudent.getYuer() - qizhongKecheng.getCprice());
            }
            qizhongKechengMapper.updateStudent(qizhongStudent);
            return "预约成功";
        }


    }

    @Override
    public void jia(QizhongPingjia qizhongPingjia) {
        qizhongKechengMapper.jia(qizhongPingjia);
    }
}
